package com.mas.strategey;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.mas.common.Consts;
import com.mas.resource.ResourceInfo;

/**
 * Extend Strategy and init with the private values of the User Agent
 */
public class UserStrategy extends Strategy{
	
	
	public UserStrategy()
	{
		privatePriceList =new HashMap<String,Double>();
		privatePriceList.put(Consts.RESOURCE_AMOUNT_TO_BUY, Consts.Defulte_User_Price_CPU);
		privatePriceList.put(Consts.RESOURCE_START_TIME, Consts.Defulte_User_Price_MEM);
		privatePriceList.put(Consts.RESOURCE_END_TIME,  Consts.Defulte_User_Price_HD );
		privatePriceList.put(Consts.RESOURCE_NET, Consts.Defulte_User_Price_NET);
		
		discountedFactor = Consts.User_DiscountedFactor;
		pricingFactor = Consts.User_pricingFactor;
	}


	public  UserStrategy(Map<String, Double> mapInitPrices)
	{
		this();
		for (Entry<String, Double> entry : mapInitPrices.entrySet()) {
			if (entry.getValue()>0){
				privatePriceList.put(entry.getKey(),entry.getValue());
			}
		}
	
	}
	
	public Consts.Response decide(Proposal proposal)
	{		
		Double totalPrivateValue = calcPrivateValue(proposal.getResourceInfos());
	
		// if the proposal is good for me, lower than my private value
		if (proposal.getPrice() <= totalPrivateValue * pricingFactor * (1 - discountedFactor) & proposal.getPrice() <= totalPrivateValue )
			return Consts.Response.ACCEPT;
	
		// if the proposal is not good for me, I will send my own offer
		return Consts.Response.PROPOSE_COUNTER;
	}
	
	protected void updateDiscountedFactor()
	{
		discountedFactor -= Consts.User_DiscountedFactorCheangesSize;
	}
	
	protected double newPrice(double myLastProposalPrice, double otherAgentProposalPrice, double myPrivateValue)
	{
		double price;
		price = (myLastProposalPrice)* (1.0 - discountedFactor);
		
		if (price >  myPrivateValue)
			return myPrivateValue;
		else
			return price;
	}

}
